package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.emp02list.EmployeeList;

public class EmployeeListZyzImpl implements EmployeeList {
    private Employee[] employees;
    private int index = 0;

    private static final int DEFAULT_INITIAL_CAPACITY = 10;

    public EmployeeListZyzImpl() {
        employees = new Employee[DEFAULT_INITIAL_CAPACITY];
    }

    public EmployeeListZyzImpl(int initialCapacity) {
        employees = new Employee[initialCapacity];
    }

    @Override
    public Employee get(int index) {
        if (index >= size()) {
            String msg = String.format("Index: %d, Size: %d",  index, size());
            throw new IndexOutOfBoundsException(msg);
        }
        return employees[index];
    }

    private void grow() {
        int currentCapacity = employees.length;
        int scaledCapacity = currentCapacity * 2;
        Employee[] newEmployees = new Employee[scaledCapacity];
        for (int i = 0; i < currentCapacity; i++) {
            newEmployees[i] = employees[i];
        }
        this.employees = newEmployees;
    }

    @Override
    public void add(Employee employee) {
        if (index >= employees.length) {
            grow();
        }
        employees[index] = employee;
        index++;
    }

    public int size() {
        return index;
    }
}
